// pages/login/tradition/index.js
import Config from "../../../main.config";
import {getStorageSync, setStorageSync} from "../../../utils/Storage";
import {isInputEmpty, isPhoneNumber} from "../../../utils/Util";
import {sendSms, register} from "../../../service/Common";
import {getRegisterDetail} from "../../../service/Enterprise";
import Notify from "@vant/weapp/notify/notify";
import Dialog from "@vant/weapp/dialog/dialog";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userType: -1, //用户类型
        phone: "", //手机号码
        yzm: "", //验证码
        smsButton: { //验证码状态
            loading: false,
            start: false
        },
        submitButton: { //提交按钮状态
            loading: false,
            loadingText: "注册中..."
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({userType: options.type ? parseInt(options.type) : -1});
        this.data._userInfo = getStorageSync(Config.storage.userInfo);
        this.data._options = options;
    },

    /*
    * 输入监听
    * */
    onInput: function (e) {
        let val = e.detail;
        let name = e.currentTarget.dataset.name;
        this.data[name] = val;
    },

    /*
    * 发送验证码
    * */
    onSendSms: async function () {
        try {
            let phone = this.data.phone;
            let loading = this.data.smsButton.loading;
            if (loading) { //防重复
                return false
            } else if (!this._checkedPhone(phone)) {
                return false
            } else {
                this.setData({"smsButton.loading": true});
                await sendSms(phone);
                this.setData({"smsButton.start": true});
            }
        } catch (e) {
            console.log(e)
        }
    },

    /*
    * 注册登录
    * */
    onSubmit: async function () {
        try {
            let options = this.data._options;
            let phone = this.data.phone;
            let yzm = this.data.yzm;
            let loading = this.data.submitButton.loading;
            if (loading) { //防重复
                return false
            } else if (!this._checkedPhone(phone)) {
                return false
            } else if (!isInputEmpty(yzm)) {
                Notify("短信验证码不能为空！");
                return false
            } else {
                let openId = getStorageSync(Config.storage.openId);
                this.setData({"submitButton.loading": true});
                let postData = {
                    account: phone,
                    openId,
                    yzm
                }
                options.createUser && options.createUser !== "undefined" ? postData.createUser = options.createUser : null
                let token = await register(postData);
                setStorageSync(Config.storage.token, token); //token 缓存
                await this._loginSuccess();
            }
        } catch (e) {
            e.message ? Dialog.alert({
                title: "登录失败",
                message: e.message || ""
            }) : null
            console.error(e)
        } finally {
            this.setData({"submitButton.loading": false});
        }
    },

    /*
    * 登录成功除了函数
    * 如果缓存信息userInfo.roleId 存在 说明用户已经注册过
    * 如果不存在 则说明用户首次注册
    * 如果首次注册的是企业用户 则下一步跳转 企业、员工身份选择
    *
    * 如果是企业用户还需要判断是否审核通过
    * 如果没有通过 需要跳转到审核页面
    * */
    _loginSuccess: async function() {
        let userType = this.data.userType;
        let userInfo = this.data._userInfo;
        let userId = userInfo && userInfo.userId ? userInfo.userId : null;
        let roleId = userInfo && userInfo.roleId ? parseInt(userInfo.roleId) : null;
        if (roleId) {
            switch (userType) {
                case 6:
                    let detail = await getRegisterDetail({createUser: userId});
                    if (detail.status !== 2) { //未通过审核
                        wx.redirectTo({url: "/packageEnterprise/pages/status/index?status=" + detail.status})
                    } else {
                        wx.redirectTo({url: "/pages/index/index"});
                    }
                    setStorageSync(Config.storage.registerInfo, detail)
                    break
                case 7:
                case 8:
                    wx.redirectTo({url: "/pages/index/index"});
                    break
                default:
                    break
            }
        } else {
            switch (userType) {
                case 6:
                case 7:
                    let invitationInfo = getStorageSync(Config.storage.invitationInfo);
                    if (invitationInfo.companyId) { //如果从公司要求信息跳转 则直接进入创建用户页面
                        wx.redirectTo({url: "/packagePersonnel/pages/info/index?todo=create"})
                    } else {
                        wx.redirectTo({url: "/pages/login/select/index"})
                    }
                    break
                case 8:
                    wx.redirectTo({url: "/packageStudent/pages/info/index?todo=create"})
                    break
                default:
                    break
            }
        }
    },


    /*
    * 验证
    * */
    _checkedPhone: function (phone) {
        if (!isInputEmpty(phone)) {
            Notify("手机号码不能为空！");
            return false
        } else if (!isPhoneNumber(phone)) {
            Notify("手机号格式不正确！");
            return false
        }
        return true
    }

})